define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coll/collection/index' + location.search,
                    // add_url: 'coll/collection/add',
                    edit_url: 'coll/collection/edit',
                    // del_url: 'coll/collection/del',
                    multi_url: 'coll/collection/multi',
                    table: 'analysis_collection',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                rowStyle:function(row, index){
                    if (row.ystatus == 2) {
                        return{
                            css:{
                                color:'red'
                            }
                        }
                    }else{
                        return{}
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'oid'},
                        {field: 'paymoney', title: __('Paymoney'), operate:false},
                        {field: 'is_income', title: __('Is_income'), searchList: $.getJSON(Config.this_url+"/coll/collection/getconfig?state=3")},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operator', title: __('Operator')},
                        {field: 'company', title: __('Company'), operate:false},
                        {field: 'settle_name', title: __('Settle_name'), operate:'LIKE'},
                        {field: 'storehouse.name', title: __('所辖仓库'), searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=1")},
                        {field: 'pay.name', title: __('收付款方式'), searchList: $.getJSON(Config.this_url+"/coll/collection/getconfig?state=4")},
                        {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'make_time', title: __('Make_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'discount', title: __('Discount'), operate:false},
                        {field: 'status', title: __('Status'), operate:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            $('table').on('dblclick','td',function(){
                var oid = $(this).parents('tr').find('.oid').html()
                var url = Config.this_url+'/coll/collection/detail?ids='+oid
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '90%'],
                    title: "单据详情",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            });
            $('.commonsearch-table').on('click','.btn-success',function () {
                $('#getcount').click()
            })
            $('#getcount').click(function () {
                var data = $('form').serialize();
                that = $(this)
                var url = Config.this_url+'/coll/collection/countget';
                $.post(url,data,function (res) {
                    that.find('#price').html(res.price)
                })
                setTimeout(function(){
                    layer.closeAll('loading');
                }, 2000);
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
            table.off('dbl-click-row.bs.table');
        },
        add: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            $('.cancelbill').click(function () {
                var url =  Config.this_url+'/coll/collection/cancelbill'+location.search
                layer.prompt({title: '输入红冲原因', formType: 2}, function(pass, index){
                    if(pass == ''){
                        layer.msg('不能为空');
                        return false;
                    }
                    $.post(url,{reason:pass},function (res) {
                        layer.msg(res.msg)
                        if(res.code == 1){
                            layer.close(index);
                            setTimeout(function () {
                                location.reload();
                            }, 800);
                            window.parent.$(".toolbar .btn-refresh").trigger('click');
                        }
                    })
                });
            })
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});